//
//  HMStatusOriginalView.swift
//  CZBKWB31
//
//  Created by Linkon Y on 16/8/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit
//原创
class HMStatusOriginalView: UIView {
    //原创微博底部记录
    var originalViewBottomConstraint: Constraint?
    
    //创建ViewModel 供外界赋值
    var statusViewModel : HMStatusViewModel?{
        didSet{
            headImageView.hm_setImageWithString(statusViewModel?.statusModel?.user?.profile_image_url, placeholderImageName: "avatar_default")
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            //时间来源后面处理
            
            timeLabel.text = statusViewModel?.sinaTimeStr

            sourceLabel.text = statusViewModel?.sinaSourceStr
            
            //微博正文
            contentLabel.attributedText = statusViewModel?.orginalAttr
            membershipImageView.image = statusViewModel?.mbrankImage
            avatarImageView.image = statusViewModel?.verifiedImage
            
            //卸载约束
            originalViewBottomConstraint?.uninstall()
            //有配图
            if statusViewModel?.statusModel?.pic_urls?.count > 0 {
                //赋值
                picView.picInfos = statusViewModel?.statusModel?.pic_urls
                self.snp_makeConstraints { (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(picView.snp_bottom).offset(10).constraint
                }
                //显示配图
                picView.hidden = false
                
            }else{
                //没有配图
                self.snp_makeConstraints { (make) in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(10).constraint
                }
                //隐藏配图
                picView.hidden = true
            }
        }
    }

       
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //设置视图
   func setupUI(){
    backgroundColor = UIColor.whiteColor()
    addSubview(headImageView)
    addSubview(nameLabel)
    addSubview(membershipImageView)
    addSubview(timeLabel) 
    addSubview(sourceLabel)
    addSubview(avatarImageView)
    addSubview(contentLabel)
    addSubview(picView)
   
    // 添加约束
    // 01
    headImageView.snp_makeConstraints { (make) -> Void in
        make.leading.top.equalTo(10)
        make.size.equalTo(CGSize(width: 35, height: 35))
    }
    // 02
    nameLabel.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(headImageView)
        make.leading.equalTo(headImageView.snp_trailing).offset(10)
    }
    // 03
    membershipImageView.snp_makeConstraints { (make) in
        make.centerY.equalTo(nameLabel)
        make.leading.equalTo(nameLabel.snp_trailing).offset(10)
    }
    // 04
    timeLabel.snp_makeConstraints { (make) in
        make.bottom.equalTo(headImageView.snp_bottom)
        make.leading.equalTo(headImageView.snp_trailing).offset(10)
    }
    // 05
    sourceLabel.snp_makeConstraints { (make) in
        make.centerY.equalTo(timeLabel)
        make.leading.equalTo(timeLabel.snp_trailing).offset(10)
    }
    // 06
    avatarImageView.snp_makeConstraints { (make) in
       make.centerY.equalTo(headImageView.snp_bottom)
       make.centerX.equalTo(headImageView.snp_trailing)
    }
    // 07
    contentLabel.snp_makeConstraints { (make) in
        make.top.equalTo(headImageView.snp_bottom).offset(10)
        make.leading.equalTo(10)
    }

   // 08
    picView.snp_makeConstraints { (make) in
        make.top.equalTo(contentLabel.snp_bottom).offset(10)
        //该约束放其内部
//        make.size.equalTo(CGSize(width: 100, height: 100))
        make.leading.equalTo((SCREENW - 250)/2)
    }
    self.snp_makeConstraints { (make) in
      originalViewBottomConstraint = make.bottom.equalTo(picView.snp_bottom).offset(10).constraint
    }

    }
    
    // MARK: - 懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(imageName: "avatar_default")
    // 名字
//    private lazy var nameLabel: UILabel = {
//        let lab = UILabel()
//        lab.font = UIFont.systemFontOfSize(15)
//        lab.textColor = UIColor.darkGrayColor()
//        return lab
//    }()
    private lazy var nameLabel : UILabel = UILabel(textColor: UIColor.darkGrayColor(), textSize: 15)
    // 用户等级
    private lazy var membershipImageView: UIImageView = UIImageView(imageName: "common_icon_membership")
    // 时间
    private lazy var timeLabel: UILabel = UILabel(textColor: AppThemeColor, textSize: 10)
    // 来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.grayColor(), textSize: 10)
    // 认证
    private lazy var avatarImageView: UIImageView = UIImageView(imageName: "avatar_vgirl")
    // 微博正文
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textSize: 15, maxWidth: SCREENW - 20)
    //配图
    private lazy var picView: HMStatusPictureView = HMStatusPictureView()
    
   
}


   